import Vue from 'vue'
import App from './App.vue'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import VueRouter from 'vue-router';
import Login from './common/Login.vue';
import axios from 'axios';
// 系统服务
import Home from './common/Home.vue'
import User from './common/User.vue'
import Role from './common/Role.vue'
// 进货服务 
import PurchaseQ from './stockpurchase/PurchaseQ.vue'
import ReturnQ from './stockpurchase/ReturnQ.vue'
import Purchase from './stockpurchase/Purchase.vue'
import Return from './stockpurchase/Return.vue'
import Inventory from './stockpurchase/Inventory.vue'
// 销售服务
import PurchaseQGoods from './salalist/PurchaseQGoods.vue'
import ReturnQGoods from './salalist/ReturnQGoods.vue'
import PurchaseGoods from './salalist/PurchaseGoods.vue'
import ReturnGoods from './salalist/ReturnGoods.vue'
import Inventorys from './salalist/Inventorys.vue'
<<<<<<< HEAD
// 统计报表
import Monthly from './statistics/Monthly.vue'
import Daily from './statistics/Daily.vue'
import Yao from './statistics/Yao.vue'
import Fukuan from './statistics/Fukuan.vue'
=======
import Sale from './salalist/Sale.vue'
>>>>>>> 935fd3a0d6ae3757684a4c6b15491219f16ca1c2
// - - - - --
import Basics from './common/Basics.vue'
import Supplier from './common/Supplier.vue'
import Customer from './common/Customer.vue'
import Goods from './common/Goods.vue'

import Gong from './tj/Gong.vue'
import EWM from './tj/EWM.vue'
import Commodity from './tj/Commodity.vue'
//引入生成二维码插件
<<<<<<< HEAD
import QRCode from "qrcode";
import echarts from 'echarts'
Vue.prototype.$echarts = echarts;
=======

//import QRCode from "qrcode";
>>>>>>> 935fd3a0d6ae3757684a4c6b15491219f16ca1c2

axios.defaults.withCredentials = true;//让ajax携带cookie
Vue.prototype.axios = axios;
//Vue.prototype.QRCode = QRCode;
Vue.use(ElementUI);
Vue.use(VueRouter);
const routes = [
  { path: "/", component: Login },
  {
    path: "/Home", component: Home,
    children: [
      {
        path: "/Home",
        name: "系统管理",
        component: Home,
        children: [{
          path: "/User",
          component: User,
          name: "用户管理"
        },
        { path: "/Role", component: Role, name: "角色管理" }
        ]
      },
      {
        path: "/Home",
        name: "进货管理",
        component: Home,
        children: [
          {
            path: "/PurchaseQ",
            name: "进货单据查询",
            component: PurchaseQ
          },
          {
            path: "/ReturnQ",
            name: "退货单据查询",
            component: ReturnQ
          },
          {
            path: "/Purchase",
            name: "进货入库",
            component: Purchase
          },
          {
            path: "/Return",
            name: "退货出库",
            component: Return
          },
          {
            path: "/Inventory",
            name: "当前库存查询",
            component: Inventory
          }
        ]
      }, {
        path: "/Home",
        name: "销售管理",
        component: Home,
        children: [
          {
            path: "/PurchaseQGoods",
            name: "销售单据查询",
            component: PurchaseQGoods
          },
          {
            path: "/ReturnQGoods",
            name: "退货单据查询",
            component: ReturnQGoods
          },
          {
            path: "/PurchaseGoods",
            name: "销售入库",
            component: PurchaseGoods
          },
          {
            path: "/ReturnGoods",
            name: "退货出库",
            component: ReturnGoods
          },
          {
            path: "/Inventorys",
            name: "当前库存查询",
            component: Inventorys
          }
        ]
      },
      {
        path: "/Basics",
        name: "基础资料",
        component: Basics,
        children: [{
          path: "/Supplier",
          component: Supplier,
          name: "供应商管理"
        },
        {
          path: "/Customer",
          component: Customer,
          name: "客户管理"
        },
        {
          path: "/Goods",
          component: Goods,
          name: "商品管理"
        }
        ]
<<<<<<< HEAD
      },
      {
        path: "/Home",
        name: "统计报表",
        component: Home,
        children: [{
          path: "/Daily",
          component: Daily,
          name: "按日统计分析"
        },
        {
          path: "/Monthly",
          component: Monthly,
          name: "按月统计分析"
        },
        {
          path: "/Yao",
          component: Yao,
          name: "新开就诊"
        },
        {
          path: "/Fukuan",
          component: Fukuan,
          name: "支付宝付款"
        },
=======
      }, {
        path: "/Home",
        name: "报表统计",
        component: Home,
        children: [{
          path: "/Sale",
          component: Sale,
          name: "销售统计"
        },
        ]
      },{
        path: "/Home",
        name: "报表统计",
        component: Home,
        children: [{
          path: "/Gong",
          component: Gong,
          name: "供应商统计"
        },
        {
          path: "/EWM",
          component: EWM,
          name: "二维码"
        },
        {
          path: "/Commodity",
          component: Commodity,
          name: "商品采购统计"
        }
>>>>>>> 935fd3a0d6ae3757684a4c6b15491219f16ca1c2
        ]
      }



    ]
  }
]

const router = new VueRouter({
  routes
})
Vue.prototype.axios = axios;


//路由的钩子函数 表示路由之间的跳转必须经过此函数
router.beforeEach((to, from, next) => {
  console.log("要跳转的路径未：---->", to.path);
  if (to.path == '/') {
    localStorage.removeItem('username');
    next();
  } else {
    if (!localStorage.getItem('username')) {
      next("/");
      return;
    }
    //请求后台查询登录用户对应的菜单
    initMenu(router);
    setTimeout(() => {
      next();
    }, 500);
  }
});

export const initMenu = (router) => {
  axios({
    url: "https://www.jxccloud.com/upms/menu/getMenuByUserName",
    method: "get",
    params: { "access_token": localStorage.getItem("access_token") }
  }).then(function (param) {
    console.log("根据当前登录用户的id动态获取权限菜单:{0}", param);
    if (param) {
      //因为后台返回的是一个List 里面的component 不是vue 识别的组件，因此需要把List 转换成Vue 识别的对象
      var routes = formRouters(param.data.obj);
      console.log("后台返回路由List 转换的结果为:{0}", routes);
      //把转换的结果赋值给 路由路径为/Home 的  children属性
      router.options.routes[1].children = routes;
      console.log("/Home children属性的值为:{0}", router.options.routes[1].children);
    }
  })
}


//参数是从Menu表查出来的 List  
// [
//   {
//   path:"/Home",
//   name:"系统管理",
//   component:Home,
//   children:[{
//     path:"/User",
//     component:User,
//     name:"用户管理"
//   },
//   {path:"/role",component:Role,name:"角色管理"}
// ]
export const formRouters = (routers) => {
  let array = [];
  routers.forEach(element => {
    let children = [];
    if (element.children && element.children instanceof Array) {
      children = formRouters(element.children);
    }
    let r = {
      path: element.path,
      component(resolve) {
        if (element.component.startsWith('User')) {
          require(['./common/' + element.component + '.vue'], resolve)
        } else if (element.component.startsWith('Role')) {
          require(['./common/' + element.component + '.vue'], resolve)
        }
      },
      name: element.name,
      children: children
    }
    array.push(r);
  });
  return array;
}



new Vue({
  el: '#app',
  router,
  render: h => h(App)
})
